import axios from "axios";
import {getToken} from "../utils/storage.js"

declare module 'axios' {
    export interface AxiosResponse<T = any> extends Promise<T> {}
}

//创建axios实例
const service = axios.create({
    baseURL: '/api',
    timeout: 5000,
    headers:{
        "Content-Type": "application/json;charset=UTF-8"
    }
})

// 请求拦截器
service.interceptors.request.use(config => {
   /*定义config.headers的类型*/
   config.headers = config.headers || {}
   if(getToken){
    config.headers.token = getToken() || ""
   }
   return config
})

//响应拦截器
service.interceptors.response.use(res => {
    /*获取后端响应的状态码*/
    const code:number = res.data.code

    // if(code != 200){
    //     return Promise.reject(res.data)
    // }

    return res.data
},(err)=>{
    /*打印错误信息*/
    console.log(err)
})



export default service